import React from 'react'
import { Form, Input, Modal, Button, message} from 'antd'
import PropTypes from 'prop-types'
import './manager.less'
import API from "../../api/person/person";
const FormItem = Form.Item

class IcsComponentsSupplierFilterForm extends React.Component {
    state = {
        showViewer: this.props.showViewer
    }
    componentDidMount () {

    }
    componentWillReceiveProps (nextProps) {
        if ('rowInfo' in nextProps) {
            this.setState({ rowInfo: nextProps.rowInfo})
        }
    }
    onCancel = () => {
        this.props.doViewer()
    }
    handleSearch = (e) => {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
            if (err) return
            if (this.props.isEditing) {
                values.pri_id = this.state.rowInfo.id
            }
            API.doRights(values).then(res => {
                if (res.error === 0) {
                    message.success('操作成功！')
                    this.props.doViewer()
                    this.props.doSearch()
                } else {
                    message.error(res.msg || '操作失败！')
                }
            })
        });
    }

    render () {
        const { getFieldDecorator } = this.props.form
        const formItemLayout = {
            labelCol: {
                xs: { span: 24 },
                sm: { span: 5 },
            },
            wrapperCol: {
                xs: { span: 24 },
                sm: { span: 17 },
            },
        }
        return this.props.showViewer && <Modal
            className="store-viewer"
            style={{width: 600}}
            visible={this.props.showViewer}
            title={this.props.isEditing ? "编辑权限" : "新建权限"}
            onOk={this.handleOk}
            onCancel={this.onCancel}
        >
            <Form
                layout="inline"
                className="supplier-filter"
                onSubmit={this.handleSearch}>
                <FormItem
                    {...formItemLayout}
                    label="权限码">
                    {getFieldDecorator('pri_code', {
                        rules: [{
                            required: true, message: '请输入权限码！',
                        }],
                        initialValue: this.state.rowInfo? this.state.rowInfo.pri_code : ''
                    })(
                        <Input />
                    )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="权限名称">
                    {getFieldDecorator('pri_name', {
                        rules: [{
                            required: true, message: '请输入权限名称！',
                        }],
                        initialValue: this.state.rowInfo? this.state.rowInfo.pri_name : ''
                    })(
                        <Input />
                    )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="权限说明">
                    {getFieldDecorator('pri_note', {
                        rules: [{
                            required: true, message: '请输入权限说明',
                        }],
                        initialValue: this.state.rowInfo? this.state.rowInfo.pri_note : ''
                    })(
                        <Input/>
                    )}
                </FormItem>

                <div className="btns">
                    <Button type="primary" style={{marginRight: 10}} onClick={this.onCancel}>取消</Button>
                    <Button type="primary" htmlType="submit">提交</Button>
                </div>
            </Form>
        </Modal>
    }
}

IcsComponentsSupplierFilterForm.propTypes = {
    getFieldDecorator: PropTypes.func,
    doSearch: PropTypes.func,
    showViewer: PropTypes.any,
    isEditing: PropTypes.bool.isRequired,
    doViewer: PropTypes.func,
    rowInfo: PropTypes.object,
    form: PropTypes.object
}

const IcsComponentsSupplierFilter = Form.create()(IcsComponentsSupplierFilterForm)

export default IcsComponentsSupplierFilter
